/*
 * Copyright (c) 2014-2015, dennis wang
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include <string>

#include "csv_coder.h"
#include "template.h"


int main(int argc, char* argv[]) {
	static const char* help =
		"file extension MUST be .csv\n"
		"-interface interface file name\n"
		"-in  CSV file directory\n"
		"-out Output file directory\n"
		"-entry CSV loader manager class name\n"
		"-file generated file name\n"
		"-namespace CSV loader namespace name\n"
		"-cocos2dx cocos2dx version"
        "-files only the files"
        "-filealias file alias"
        "-config config file"
        "-exorcise exorcise files"
        ;
    if (argc < 2) {
        std::cout << help;
        return 0;
    }

    csvDefine define;
    for (int i = 1; i < argc; i++) {
        if (argv[i] == std::string("-cocos2dx")) {
            g_include_files = include_files_cocos2dx;
            g_singleton_load_begin = singleton_load_begin_cocos2dx;
            std::cout << "generate for cocos2dx" << std::endl;
        } else if (argv[i] == std::string("-config")) {
            define.configfile = argv[i + 1];
            define.loadConfig();
            i += 1;
        }
    }
    if (define.in.empty()) {
        define.in = ".";
    }
    if (define.out.empty()) {
        define.out = ".";
    }
    for (int i = 1; i < argc; i++) {
        if (argv[i] == std::string("-cocos2dx")) {
            continue;
        } else if (argv[i] == std::string("-config")) {
            i += 1;
            continue;
        } else {
            std::string args = argv[i];
            if (args.length() > 1 && args[0] == '-') {
                args = args.substr(1);
            }
            std::string* pCfg = define.getConfig(args);
            if (pCfg != NULL) {
                *pCfg = argv[i + 1];
            } else {
                std::cout << help;
            }
            i += 1;
        }
    }

    if (define.np.empty()) {
        define.np = "CsvCoder";
	}

    std::cout << "input directory:  " << define.in << std::endl;
    std::cout << "output directory: " << define.out << std::endl;
    std::cout << "namespace :" << define.np << std::endl;
    std::cout << "files :" << define.files << std::endl;
    std::cout << "config :" << define.configfile << std::endl;
	std::cout << "generating..." << std::endl;

    CsvCoder coder(define);
    if (define.interface.empty()) {
        if (!coder.generateCsvAccess(define.files, define.filealias, define.exorcise)) {
            std::cout << "failed..." << std::endl;
        } else {
            std::cout << "finished..." << std::endl;
        }
    } else {
        if (!coder.generateCsvInterface(define.interface)) {
            std::cout << "failed..." << std::endl;
        } else {
            std::cout << "finished..." << std::endl;
        }
    }

    return 0;
}
